package net.rlair.sbdemo.entity;

import org.springframework.data.domain.Pageable;

import java.util.List;

public class Page<T> {

    /**
     * begin.
     */
    private int begin;
    /**
     * end.
     */
    private int end;
    /**
     * 页大小
     */
    private int length = 20;
    /**
     * 总记录数
     */
    private int totalRecords;
    /**
     * 页码
     */
    private int pageNo;
    /**
     * 页数.
     */
    private int pageCount;

    private List<T> list;

    public Page() {
    }

    public Page(int pageNo, int length) {
        pageNo = pageNo > 0 ? pageNo : 1;
        this.pageNo = pageNo;
        this.length = length;
        this.begin = this.length * (pageNo - 1);
        this.end = this.length * pageNo;
    }

    public Page(int totalRecords, int pageNo, List<T> list) {
        setTotalRecords(totalRecords);
        setPageNo(pageNo);
        this.list = list;
    }

    public List<T> getList() {
        return list;
    }

    public void setList(final List<T> list) {
        this.list = list;
    }

    public int getBegin() {
        return this.begin;
    }

    public int getEnd() {
        return this.end;
    }

    public void setEnd(int end) {
        this.end = end;
    }

    public void setBegin(int begin) {
        this.begin = begin;
        if (this.length != 0) {
            this.pageNo = (int)Math.floor((double)this.begin * 1.0D / (double)this.length) + 1;
        }

    }

    public int getLength() {
        return this.length;
    }

    public void setLength(int length) {
        this.length = length;
    }

    public int getTotalRecords() {
        return this.totalRecords;
    }

    public void setTotalRecords(int totalRecords) {
        this.totalRecords = totalRecords;
        this.pageCount = (int)Math.floor((double)this.totalRecords * 1.0D / (double)this.length);
        if (this.totalRecords % this.length != 0) {
            ++this.pageCount;
        }

    }

    public int getPageNo() {
        return this.pageNo;
    }

    public void setPageNo(int pageNo) {
        this.pageNo = pageNo;
        pageNo = pageNo > 0 ? pageNo : 1;
        this.begin = this.length * (pageNo - 1);
        this.end = this.length * pageNo;
    }

    public int getPageCount() {
        return this.pageCount == 0 ? 1 : this.pageCount;
    }

    public void setPageCount(int pageCount) {
        this.pageCount = pageCount;
    }

    public String toString() {
        StringBuilder builder = (new StringBuilder("begin=")).append(this.begin).append(", end=").append(this.end).append(", length=").append(this.length).append(", totalRecords=").append(this.totalRecords).append(", pageNo=").append(this.pageNo).append(", pageCount=").append(this.pageCount);
        return builder.toString();
    }
}
